﻿using CommonTools;
using shop.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.ModelBinding;


namespace shop.Bll.trade
{
    /// <summary> 出库明细 </summary>
    public class StockOutBll
    {

        /// <summary> 保存出库记录 </summary>
        public async Task<Result> SaveStockOutItems(IEnumerable<OutStockItem> items)
        {
            if(items == null || items.Any()== false)
            {
                return new Result(true);
            }

            //检查不要重复添加 
            //删除旧的
            var arr = items.Select(a => a.outstock_no).ToArray();

            await DB.FSql.Delete<OutStockItem>()
                .Where(a => arr.Contains(a.outstock_no))
                .ExecuteAffrowsAsync();

            await DB.FSql.Insert(items)
                .ExecuteSqlBulkCopyAsync();
            
            return new Result(true);
        }


        /// <summary> 查询本地出库数据 </summary>
        public async Task<PageQueryResult<dynamic>> QueryOutStock(DateTime begin, DateTime end, int pageNo, int pageSize,
            OutStockItem pr)
        {
            begin = begin.Date;
            end = end.LastTimeOfDay();

            //快递老板
            var logiInfo = await DB.FSql.Select<LogiInfo>()
                .Where(a => a.isdel == false && a.logistics_no != string.Empty)
                .ToListAsync(a => new
                {
                    a.logistics_no,
                    a.LogiAcct,
                    a.IsAgent,
                    a.logistics_name,
                });

            var list = (await DB.FSql.Select<OutStockItem>()
                .Where(a => a.consign_time >= begin && a.consign_time <= end)
                .WhereIf(!string.IsNullOrEmpty(pr.shop_name), a=> a.shop_name.Contains(pr.shop_name))
                .WhereIf(!string.IsNullOrEmpty(pr.logistics_no), a=> a.logistics_no.Contains(pr.logistics_no))
                .WhereIf(!string.IsNullOrEmpty(pr.trade_no), a=> a.trade_no.Contains(pr.trade_no))
                .WhereIf(!string.IsNullOrEmpty(pr.outstock_no), a=> a.outstock_no.Contains(pr.outstock_no))
                .WhereIf(!string.IsNullOrEmpty(pr.goods_name), a=> a.goods_name.Contains(pr.goods_name))
                .Count(out var c)
                .Page(pageNo, pageSize)
                .ToListAsync() )
                .Select(a => {
                    var info = (logiInfo.FirstOrDefault(x => x.logistics_name == a.logistics_name));
                    return new
                    {
                        a.Id,
                        快递老板 = info?.LogiAcct,
                        是否代发 = info?.IsAgent ?? false,
                        物流单号 = a.logistics_no,
                        单品支付金额 = a.goodsAmount,
                        仓库 = a.warehouse,
                        商家编码 = a.spec_no,
                        订单编号 = a.trade_no,
                        原始单号 = a.src_tids,
                        客服备注 = a.cs_remark,
                        子单原始单号 = a.src_tid,
                        原始子订单号 = a.src_oid,
                        订单类型 = a.trade_type,
                        支付账号 = a.pay_account,
                        出库单编号 = a.outstock_no,
                        仓库类型 = a.warehouse_type,
                        打单员 = a.printer,
                        店铺 = a.shop_name,
                        出库单状态 = a.status,
                        出库状态 = a.consign_status,
                        分拣序号 = a.picklist_seq,
                        货品编号 = a.goods_no,
                        货品名称 = a.goods_name,
                        货品简称 = a.goods_shot_name,
                        品牌 = a.brand_name,
                        分类 = a.goods_group,
                        规格码 = a.spec_code,
                        规格名称 = a.spec_name,
                        条形码 = a.barcode,
                        货品数量 = a.num,
                        货品原单价 = a.goods_original_price,
                        货品原总金额 = a.goods_original_amount,
                        订单总优惠 = a.order_amount,
                        邮费 = a.post_amount,
                        货品成交价 = a.sell_price,
                        货品成交总价 = a.sell_amount,
                        货品总优惠 = a.discount,
                        货到付款金额 = a.cod_amount,
                        货品成本 = a.cost_price,
                        货品总成本 = a.cost_amount,
                        订单支付金额 = a.paid,
                        应收金额 = a.receivable,
                        退款前支付金额 = a.paid_before_return,
                        分摊邮费 = a.share_post,
                        预估邮资 = a.calc_post_cost,
                        邮资成本 = a.post_fee,
                        订单包装成本 = a.package_cost,
                        订单毛利 = a.profit,
                        毛利率 = a.profit_rate,
                        客户网名 = a.customer_nick_name,
                        收件人 = a.receiver_name,
                        证件号码 = a.id_card,
                        收货地区 = a.receiver_position,
                        收货地址 = a.receiver_address,
                        收件人手机 = a.receiver_phone,
                        收件人电话 = a.receiver_phone2,
                        物流公司 = a.logistics_name,
                        实际重量 = a.weight,
                        预估重量 = a.calc_weight,
                        需开发票 = a.invoicing_required,
                        制单人 = a.operator_name,
                        拣货员 = a.picker_name,
                        打包员 = a.packager_name,
                        检视员 = a.checker,
                        业务员 = a.salesman_name,
                        验货员 = a.examiner_name,
                        打印波次 = a.print_no,
                        物流单打印状态 = a.logistics_print_status,
                        发货单打印状态 = a.sendbill_print_status,
                        分拣单打印状态 = a.picklist_print_status,
                        分拣单编号 = a.picklist_no,
                        外部单号 = a.outer_no,
                        付款时间 = a.pay_time,
                        发货时间 = a.consign_time,
                        赠品方式 = a.gift_methods,
                        买家留言 = a.buyer_message,
                        打印备注 = a.print_remark,
                        备注 = a.remark,
                        包装 = a.is_package,
                        来源组合装编码 = a.suite_no,
                        拆自组合装 = a.self_assembly,
                        来源组合装数量 = a.from_assembly_qty,
                        体积 = a.volumetric,
                        分销商 = a.fenxiao_nick,
                        下单时间 = a.trade_time,
                        审核时间 = a.stock_check_time,
                        分销商编号 = a.fenxiao_no,
                    };
                }).ToList();
                

            return new PageQueryResult<dynamic>(true, null, list, c);
        }






    }
}
